package com.ztgf.sys.api.cms;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.sys.models.bo.SysConfigBO;
import com.ztgf.sys.models.dto.SysConfigDTO;
import com.ztgf.sys.models.service.ISysConfigService;
import com.ztgf.sys.models.vo.SysConfigVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * 数据字典表控制器
 *
 * @author mxli
 * @Date 2020-05-27 10:03:34
 */

@RestController
@RequestMapping("/cms/sysConfig")
@Slf4j
@Api(value = "SysConfigController",tags = "数据字典表接口")
public class SysConfigController {

    @DubboReference
    private ISysConfigService sysConfigService;

    @ApiOperation(value ="根据code编码获取数据字典",notes = "根据code编码获取数据字典")
    @PostMapping(value = "/getByCode")
    public Result<SysConfigDTO> getByCode(@RequestParam("code") String code){
        if(null == code){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        SysConfigDTO dto = sysConfigService.getByCode(code);

        return new Result<>(dto);
    }

    @ApiOperation(value = "添加数据字典表",notes = "添加数据字典表")
    @PostMapping(value = "/save")
    public Result save(String str) {
        SysConfigBO bo = JSONObject.parseObject(str,SysConfigBO.class);
        boolean flag = sysConfigService.insert(bo);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "修改数据字典表",notes = "修改数据字典表")
    @PostMapping("/update")
    public Result update(SysConfigBO bo){
        boolean flag = sysConfigService.updateById(bo);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "修改状态",notes = "修改状态")
    @ApiImplicitParams({
             @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
             ,@ApiImplicitParam(name = "status",value = "状态 0未启用，1启用",required = true,paramType = "query", dataType = "Integer")
     })
     public Result updateStatus(Long id, Integer status){
         SysConfigDTO dto = new SysConfigDTO();
         dto.setId(id);
         dto.setStatus(status);
        boolean flag = sysConfigService.updateById(dto);
         if(flag){
             return new Result<>();
         } else {
             return new Result<>(CodeEnum.FAIL_100008);
         }
     }

    @ApiOperation(value = "删除数据字典表",notes = "删除数据字典表")
    @ApiImplicitParam(name = "id",value = "系统配置表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = sysConfigService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除数据字典表",notes = "批量删除数据字典表")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        sysConfigService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<SysConfigVO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        SysConfigVO sysConfigVO = sysConfigService.selectById(id);

        return new Result<>(sysConfigVO);
    }

    @ApiOperation(value = "数据字典表列表",notes = "数据字典表列表")
    @PostMapping("/queryPageList")
    public Result<Page<SysConfigDTO>> queryPageList(SysConfigBO bo){
        Page<SysConfigDTO> page = sysConfigService.getByInfoList(bo);
        return new Result<>(page);
    }
}
